import React from 'react';
import { Modal, Button, Form, Input, Switch, Select } from 'antd';
import styles from './index.less';

const Item = Form.Item;
const Option = Select.Option;
const TextArea = Input.TextArea;

@Form.create()
class Components extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  /**
   * 提交表单到父组件
   */
  onOk = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        values.openState = values.openState ? 1 : 0;
        this.props.submit(values);
      }
    });
  };
  clear = () => {
    this.props.form.resetFields();
  };

  render() {
    const {
      visible,
      formatData,
      close,
      list,
      type,
      form: { getFieldDecorator },
    } = this.props;
       

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    };
    return (
      <Modal
        title="管理域名"
        afterClose={this.clear}
        visible={visible}
        onCancel={close}
        okText="确定"
        cancelText="取消"
        onOk={this.onOk}
      >
        <div className={styles.formWrap}>
          <Form>
            {type === "edit" ? (
              <Item {...formItemLayout} label="域名">
                {getFieldDecorator('domain', {
                  rules: [{ required: true, message: '不能为空' }],
                  initialValue: formatData && formatData.domain,
                })(<Input />)}
              </Item>
            ) : (
              <Item {...formItemLayout} label="域名">
                {getFieldDecorator('domain', {
                  rules: [{ required: true, message: '不能为空' }],
                })(<Input.TextArea placeholder="每行一个域名，可添加多行" rows={5} />)}
              </Item>
            )}
            <Item {...formItemLayout} label="所属域名组">
              {getFieldDecorator('domainGroupId', {
                rules: [{ required: true, message: '不能为空' }],
                initialValue: formatData && formatData.domainGroupId,
              })(
                <Select>
                  {list.map(item => (
                    <Option value={item.id} key={item.id}>
                      {item.groupTitle}
                    </Option>
                  ))}
                </Select>
              )}
            </Item>
            <Item {...formItemLayout} label="备注">
              {getFieldDecorator('remark', {
                initialValue: formatData && formatData.remark,
              })(<Input />)}
            </Item>
            <Item {...formItemLayout} label="启用状态">
              {getFieldDecorator('openState', {
                valuePropName: 'checked',
                initialValue: formatData && formatData.openState ? true : false,
              })(<Switch checkedChildren="开" unCheckedChildren="关" />)}
            </Item>
          </Form>
        </div>
      </Modal>
    );
  }
}

export default Components;
